//! @author Adam Emil Skoog
//! @date   2011-12-24

#include "MouseInputEventReceiver.h"

namespace kedja
 {
	/**
	 * Constructor.
	 */
	MouseInputEventReceiver::MouseInputEventReceiver()
	:
	onMouseButtonPress  (NULL),
	onMouseButtonRelease(NULL)
	 {
		// Make sure that the class has been initialised.
		assert(static_cast<bool>(mouseInputEventReceivers));

		if (mouseInputEventReceivers)
			mouseInputEventReceivers->push_back(this);
	 }

	/**
	 * Registers a function to invoke when this event is invoked,
	 * when a mouse button is pressed.
	 * 
	 * @param function The function to register.
	 */
	void MouseInputEventReceiver::registerOnMouseButtonPress(void (*function)(mouse::Button const &))
	 {
		onMouseButtonPress = function;
	 }

	/**
	 * Deregisters the function registered for this event.
	 */
	void MouseInputEventReceiver::deregisterOnMouseButtonPress()
	 {
		onMouseButtonPress = NULL;
	 }

	/**
	 * Registers a function to invoke when this event is invoked,
	 * when a mouse button is released.
	 * 
	 * @param function The function to register.
	 */
	void MouseInputEventReceiver::registerOnMouseButtonRelease(void (*function)(mouse::Button const &))
	 {
		onMouseButtonRelease = function;
	 }

	/**
	 * Deregisters the function registered for this event.
	 */
	void MouseInputEventReceiver::deregisterOnMouseButtonRelease()
	 {
		onMouseButtonRelease = NULL;
	 }

	/**
	 * Initialises the receiver's system.
	 */
	void MouseInputEventReceiver::initialise()
	 {
		// Prohibit double initialisation.
		assert(!static_cast<bool>(mouseInputEventReceivers));

		if (!mouseInputEventReceivers)
			mouseInputEventReceivers = new std::vector<MouseInputEventReceiver *>;
	 }

	/**
	 * Deinitialises the receiver's system.
	 */
	void MouseInputEventReceiver::deinitialise()
	 {
		// Prohibit double deinitialisation.
		assert(static_cast<bool>(mouseInputEventReceivers));

		if (mouseInputEventReceivers)
		 {
			delete mouseInputEventReceivers;
			mouseInputEventReceivers = NULL;
		 }
	 }

	/**
	 * Returns the mouse input event receiver at the specified index.
	 *
	 * @param index The index from which to retrieve the receiver.
	 * @return The receiver at the specified index.
	 */
	MouseInputEventReceiver *const MouseInputEventReceiver::getMouseInputEventReceiver(const unsigned &index)
	 {
		// Make sure that the class has been initialised.
		assert(static_cast<bool>(mouseInputEventReceivers));

		// Make sure there are any parents at all.
		assert((mouseInputEventReceivers->size() > 0));

		// Validate the index.
		assert((index < mouseInputEventReceivers->size()));

		if (mouseInputEventReceivers)
			if (mouseInputEventReceivers->size() > 0)
				if (index < mouseInputEventReceivers->size())
					return (*mouseInputEventReceivers)[index];

		return NULL;
	 }

	/**
	 * Returns the number of mouse input event receivers.
	 *
	 * @return The number of receivers.
	 */
	const unsigned MouseInputEventReceiver::getMouseInputEventReceiverCount()
	 {
		// Make sure that the class has been initialised.
		assert(static_cast<bool>(mouseInputEventReceivers));

		if (mouseInputEventReceivers)
			return mouseInputEventReceivers->size();

		return 0;
	 }

	// Define static variables.
	std::vector<MouseInputEventReceiver *> *MouseInputEventReceiver::mouseInputEventReceivers = NULL;
 }
